// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.reports.editor.design.parts;

import java.beans.PropertyChangeEvent;
import java.util.List;

import org.dengues.reports.editor.design.border.ReportDesignMarginBorder;
import org.dengues.reports.editor.design.figures.ReportRootFigure;
import org.dengues.reports.editor.design.layout.ReportRootToolbarLayout;
import org.dengues.reports.editor.design.models.ReportsDiagram;
import org.dengues.reports.editor.design.policies.ReportContainerEditPolicy;
import org.dengues.reports.editor.design.policies.ReportFlowLayoutEditPolicy;
import org.dengues.reports.editor.design.tools.RootDragTracker;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.DragTracker;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.requests.SelectionRequest;
import org.eclipse.gef.tools.DeselectAllTracker;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * <p>
 * Report design editPart This is the content edit part for report Designer. All other report elements puts on to it
 * </p>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-2-22 qiang.zhang $
 * 
 */
public class ReportDesignEditPart extends ReportElementEditPart {

    protected boolean showMargin = true;

    /**
     * constructor
     * 
     * @param obj the object
     */
    public ReportDesignEditPart(Object obj) {
        super(obj);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractEditPart#getModelChildren()
     */
    @Override
    protected List getModelChildren() {
        return ((ReportsDiagram) getModel()).getRepTables();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
     */
    @Override
    protected IFigure createFigure() {
        ReportRootFigure figure = new ReportRootFigure();
        figure.setOpaque(true);
        figure.setShowMargin(showMargin);

        ReportRootToolbarLayout layout = new ReportRootToolbarLayout(this);

        Dimension size = new Dimension(800, 1200);
        Rectangle bounds = new Rectangle(0, 0, size.width - 1, size.height - 1);

        layout.setInitSize(bounds);

        layout.setInitInsets(new Insets(60, 40, 60, 40));
        figure.setLayoutManager(layout);

        figure.setBorder(new ReportDesignMarginBorder(new Insets(50, 50, 50, 50)));

        // figure.setBounds(bounds.getCopy());

        return figure;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
     */
    @Override
    protected void createEditPolicies() {
        installEditPolicy(EditPolicy.LAYOUT_ROLE, new ReportFlowLayoutEditPolicy());
        installEditPolicy(EditPolicy.CONTAINER_ROLE, new ReportContainerEditPolicy());
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.EditPart#getDragTracker(org.eclipse.gef.Request)
     */

    @Override
    public DragTracker getDragTracker(Request req) {
        if (req instanceof SelectionRequest && ((SelectionRequest) req).getLastButtonPressed() == 3)
            return new DeselectAllTracker(this);
        return new RootDragTracker();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.birt.designer.internal.ui.editors.schematic.editparts.AbstractReportEditPart#refreshFigure()
     */
    @Override
    public void refreshFigure() {

    }

    /*
     * (non-Javadoc)
     * 
     * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
     */
    public void propertyChange(PropertyChangeEvent evt) {
        if (ReportsDiagram.PROP_REPTABLES.equals(evt.getPropertyName())) {
            refreshChildren();
        }

    }

}